<?php
namespace app\system\controller;

use app\system\model\NewClassModel;
use app\system\model\NewsModel;

/**
 * 文章分类
 * 
 * @author zy
 **/

class News extends Common
{

    /**
     * 列表
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $classModel = new NewClassModel();
		$newclass_id = input('newclass_id', 0, 'intval');
		$keywords = input ('keywords', '', 'trim');
        if ($classModel->where('id', $newclass_id)->count() <= 0) {
            return $this->error('文章分类不存在!', 'Newclass/index');
        }

        $where = [];
        $newclass_id > 0 && $where['newclass_id'] = $newclass_id;
        !empty($keywords) && $where['title'] = ['like', "%{$keywords}%"];

		$model = new NewsModel();
        $list = $model->selectNews($where, 20, 'sort desc,id desc');

        $newClassList = $classModel->order('sort desc,id desc')->select();
        $newClassList = classsorting($newClassList);
        $classList = [];
        foreach ($newClassList as $k=>$v){
            $v['end'] = $classModel->where(['pid'=>$v['id']])->count() > 0 ? true : false;
            $classList[$v['id']] = $v;
        }

        $assign = [
            'newClassList' => $classList,
            'list' => $list,
            'newclass_id' => $newclass_id
        ];
        return $this->fetch('', $assign);
    }

    /**
     * 添加文章
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
    	if ( isAjaxPost() ) {
            
        	$data = input('post.');
            $id = $data['id'];
            $classModel = new NewClassModel();
            if ($classModel->where(['pid'=>$data['newclass_id']])->count() > 0) {
                return $this->jsonError('只有最底层的分类才可以添加文章');
            }

        	$validate = new \app\system\validate\News();
            if (!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            $model = new NewsModel();
            if ($id > 0) {
                if ($model->where(['id'=>$id])->update($data) !== false) {
                    return $this->jsonSuccess('修改成功');
                } else {
                    return $this->jsonError('修改失败');
                }
            } else {
                unset($data['id']);
                $data['time'] = time();
                if ($model->insert($data)) {
                    return $this->jsonSuccess('添加成功');
                } else {
                    return $this->jsonError('添加失败');
                }
            }
        } else {
            $newclass_id = input('param.newclass_id');
            $id = input('param.id', '', 'intval');
            if ($id > 0) {
                $model = new NewsModel();
                $data = $model->where('id', $id)->find();
                $newclass_id = $data['newclass_id'];
                $this->assign('data', $data);
            }
            ! $newclass_id && $this->error('参数错误！');
            $this->assign('newclass_id', $newclass_id);
            return $this->fetch();
        }
    }
    
    
    /**
     * 删除文章
     * */
    public function del(){
        if (isAjaxPost()) {
            $id = input('post.id');

            $model = new NewsModel();
            if ($id > 0 && $model->where('id', $id)->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }
    
    /**
     * 批量删除文章
     * */
    public function delAll()
    {
        if (isAjaxPost()) {
            $post = input('post.');

            if ($post['newclass_id'] <= 0) {
                return $this->jsonError('参数有误！');
            }
            if (empty($post['id']) || !is_array($post['id']) || count($post['id']) <= 0) {
                return $this->jsonError('请勾选需要删除的文章！');
            }
            $ids = $post['id'];

            $model = new NewsModel();
            if ($model->where(['id'=>['in', $ids], 'newclass_id'=>$post['newclass_id']])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    /**
     * 复制文章
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function copy(){
        if (isAjaxPost()) {
            $post = input('post.');
            $class_id = input('get.class_id', 0, 'intval');

            if ($post['newclass_id'] <= 0 || $class_id <= 0) {
                return $this->jsonError('参数有误！');
            }
            if (empty($post['id']) || !is_array($post['id']) || count($post['id']) <= 0) {
                return $this->jsonError('请勾选需要复制的文章！');
            }
            $ids = $post['id'];

            $model = new NewsModel();
            $list = $model->field('id',true)->where(['id'=>['in', $ids]])->select();
            foreach ($list as $k => &$v){
                $v = $v->toArray();
                $v['newclass_id'] = $class_id;
            }
            if ($model->insertAll($list)) {
                return $this->jsonSuccess('复制成功');
            } else {
                return $this->jsonError('复制失败');
            }
        }
    }

    /**
     * 移动文章
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function mobile(){
        if (isAjaxPost()) {
            $post = input('post.');
            $class_id = input('get.class_id', 0, 'intval');

            if ($post['newclass_id'] <= 0 || $class_id <= 0) {
                return $this->jsonError('参数有误！');
            }
            if (empty($post['id']) || !is_array($post['id']) || count($post['id']) <= 0) {
                return $this->jsonError('请勾选需要移动的文章！');
            }
            $ids = $post['id'];

            $model = new NewsModel();
            if ($model->where(['id'=>['in', $ids], 'newclass_id'=>$post['newclass_id']])->setField('newclass_id',$class_id) !== false) {
                return $this->jsonSuccess('移动成功');
            } else {
                return $this->jsonError('移动失败');
            }
        }
    }
    
    /**
     * 排序
     * */
    public function saveSort()
    {
        if (isAjaxPost()) {
            $model = new NewsModel();
            $sort = input('post.');
            foreach ($sort as $k => $v) {
                $model->where('id', $k)->update(['sort' => $v]);
            }
            return $this->jsonSuccess('修改成功');
        }
    }
    
}